package com.fit.pages.registration;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.client.ClientDAO;
import com.fit.dao.company.CompanyDAO;
import com.fit.dao.registration.LicencePlateDAO;
import com.fit.dao.vehicle.VehicleDAO;
import com.fit.entities.Client;
import com.fit.entities.LicencePlate;
import com.fit.entities.Vehicle;
import com.fit.model.type.VehicleType;

@UserAccess
@AdminAccess
public class EditRegistration {

	@InjectPage
	private ManageRegistrations manage;

	@Persist
	@Property
	private Date date;

	@Property
	@Persist
	private Vehicle vehicle;

	@Inject
	private VehicleDAO vehicleDAO;

	@Property
	@SessionState(create = false)
	private LicencePlate registration;

	@Inject
	private LicencePlateDAO registrationDAO;

	@Inject
	private Messages messages;

	@Persist
	@Property
	private String error;

	@Persist
	@Property
	private String confirmation;

	@Property
	@Persist
	private VehicleType vehicleType;

	@Property
	@Persist
	private Client client;

	@Inject
	private ClientDAO clientDAO;


	public SelectModel getTypes() {
		return new EnumSelectModel(VehicleType.class, messages);
	}

	Object onActivate(String licencePlateNo) {
		manage.setError("");
		manage.setConfirmation("");
		LicencePlate r = registrationDAO.findByLicencePlateNo(licencePlateNo);
		if (r != null) {
			registration = r;
			vehicle = r.getVehicle();
			client = r.getClient();
		}
		if (registration == null) {
			manage.setError("Error");
			return manage;
		}
		return null;
	}

	Object onSubmitFromCreateForm() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			if (vehicleType != null
					&& !vehicle.getTypeOfVehicle().equalsIgnoreCase(
							vehicleType.toString())) {
				vehicle.setTypeOfVehicle(vehicleType.toString());
			}
			vehicleDAO.update(vehicle);
			manage.setConfirmation("Vehicle successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}
	}

	Object onSubmitFromCreateForm1() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			clientDAO.update(client);
			manage.setConfirmation("Client successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}
	}

	Object onSubmitFromCreateForm2() {
		manage.setError("");
		manage.setConfirmation("");
		try {
			registrationDAO.update(registration);
			manage.setConfirmation("Successfully edited.");
			return manage;
		} catch (Exception e) {
			manage.setError("Error.");
			return manage;
		}

	}

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation = "";
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error = "";
		return null;
	}

	public boolean isErrorNotNull() {
		if (error == null || error == "") {
			return false;
		}
		return true;
	}

	public boolean isConfirmationNotNull() {
		if (confirmation == null || confirmation == "") {
			return false;
		}
		return true;
	}
}
